<?php

/**
* AdminShowRouteTest.php
*
* Show route test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminShowRouteTest extends WebTestCase
{
    private $testName = array();
    private $idArray = array();
    private $username = '';
    private $password = '';

    public function setUp() 
    {


        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[0]."','".$this->username."' )");
        $this->idArray['guide_id'] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray['guide_id']."', '".$this->testName[1]."' ,'".$this->username."' )");
        $this->idArray['slide_1'] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values (  '".$this->idArray['guide_id']."', '".$this->testName[2]."','".$this->username."' )");
        $this->idArray['slide_2'] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values (  '".$this->idArray['guide_id']."', '".$this->testName[3]."','".$this->username."' )");
        $this->idArray['slide_3'] = $dbh->lastInsertId();

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url.'?cmd=AdminGuideList');
        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminShowRoute&id='.$this->idArray['guide_id']);
    }

    public function testGuideList()
    {

        $global = &PEAR::getStaticProperty("CONFIG_INI","GLOBAL");        
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertText('Trasa:');
        $this->assertText('Slajdy');
        
        $this->assertLink('[Dodaj slajd]',$global['service_url']."index.php?cmd=AdminAddSlide&guide_id=".$this->idArray['guide_id']);
        
        $this->assertLink($this->testName[1],$global['service_url']."index.php?cmd=AdminAddSlide&id=".$this->idArray['slide_1']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLink($this->testName[2],$global['service_url']."index.php?cmd=AdminAddSlide&id=".$this->idArray['slide_2']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLink($this->testName[3],$global['service_url']."index.php?cmd=AdminAddSlide&id=".$this->idArray['slide_3']."&guide_id=".$this->idArray['guide_id']);

    }

    public function testClickRemove()
    {

        $global = &PEAR::getStaticProperty("CONFIG_INI","GLOBAL");        
     
        $this->assertLinkbyId('AdminRemoveSlide'.$this->idArray['slide_1'].$this->idArray['guide_id'],$global['service_url']."index.php?cmd=AdminRemoveSlide&id=".$this->idArray['slide_1']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLinkbyId('AdminRemoveSlide'.$this->idArray['slide_2'].$this->idArray['guide_id'],$global['service_url']."index.php?cmd=AdminRemoveSlide&id=".$this->idArray['slide_2']."&guide_id=".$this->idArray['guide_id'],$global['service_url']."index.php?cmd=AdminRemoveSlide&id=".$this->idArray['slide_2']."&guide_id=".$this->idArray['guide_id']);
        $this->assertLinkbyId('AdminRemoveSlide'.$this->idArray['slide_3'].$this->idArray['guide_id'],$global['service_url']."index.php?cmd=AdminRemoveSlide&id=".$this->idArray['slide_3']."&guide_id=".$this->idArray['guide_id']);

        $this->clickLink('[Usuń]');

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');
        $this->assertField('guide_id', $this->idArray['guide_id']);
        $this->assertField('id', $this->idArray['slide_1']);
    }


    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        
        $dbh->exec("DELETE FROM guide WHERE rowid = ".$this->idArray['guide_id']  );
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_1']  );
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_2']  );
        $dbh->exec("DELETE FROM slide WHERE rowid = ".$this->idArray['slide_3']  );
        $dbh = null;

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
    }
    
}
?>
